package com.hyts.serialize.json.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hyts.serialize.json.JsonSerializeHandler;

import java.io.IOException;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.serialize.json.jackson
 * author:Libo/Alex
 * create-date:2019-06-28 11:07
 * email:liboware@gmail.com
 * description:此类主要用于: Jackson服务对象
 */
public class JacksonSerializeHandler implements JsonSerializeHandler<ObjectMapper>{


    private static ObjectMapper mapper = new ObjectMapper();


    @Deprecated
    @Override
    public <P> ObjectMapper buildJSONObject(P param) {
        return mapper;
    }

    @Override
    public <P> String buildJSONString(P param) {
        try {
            return mapper.writeValueAsString(param);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Deprecated
    @Override
    public <P> ObjectMapper buildJSONObject(String jsonStr) {
        return mapper;
    }

    @Override
    public <P> P buildBean(String jsonStr, Class<P> clazzType) {
        try {
            return mapper.readValue(jsonStr,clazzType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object buildBean(String jsonStr) {
        return buildBean(jsonStr,Object.class);
    }
}
